<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Controllers\AdminController;

abstract class CommonConfigController extends AdminController
{
    abstract protected function getConfigTag();

    abstract protected function customGrid();
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Config(), function (Grid $grid) {
            $grid->model()->where('tag', $this->getConfigTag());

            $grid->id->sortable();
            $grid->key;
            $grid->value->width('300px');
            $grid->cname;
            $grid->tag;

            $this->customGrid();

            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    abstract protected function customForm();

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Config(), function (Form $form) {
            $form->hidden('tag')->default($this->getConfigTag());

            $form->display('id');
            $form->text('key');
            $form->text('value');
            $form->text('cname');

            $this->customForm();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
